package cn.nubia.admin.sysconfig.service;

import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.mybatis.bean.Query;
import cn.nubia.admin.common.mybatis.service.BaseServiceImpl;
import cn.nubia.admin.common.springmvc.WebContext;
import cn.nubia.admin.common.utils.DateUtil;
import cn.nubia.admin.sysconfig.bean.SystemLog;
import cn.nubia.admin.sysconfig.query.SystemLogQuery;
import org.springframework.stereotype.Service;

/**
 * Created by justin on 15/8/19.
 */
@Service
public class SystemLogService  extends BaseServiceImpl<SystemLog> {


    public void log(SystemLog.ModuleTypeEnum moduleTypeEnum,SystemLog.OperateTypeEnum operateTypeEnum,Integer objectId){
        try{
            SystemLog systemLog = new SystemLog();
            Integer adminId = WebContext.getCachedUser().getId();//WebContext.getCachedUser().getId()
            String  adminName = WebContext.getCachedUser().getName();//WebContext.getCachedUser().getName()
            systemLog.setCreateTime(DateUtil.getCurrentTime());
            systemLog.setCreateId(adminId);
            systemLog.setCreateName(adminName);
            systemLog.setModuleType(moduleTypeEnum.value);
            systemLog.setOperateType(operateTypeEnum.value);
            systemLog.setObjectId(objectId);
            insert(systemLog);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public Paged<SystemLog> findPagedByQuery(SystemLogQuery systemLogQuery) {
        Query<SystemLog> query = Query.build(SystemLog.class);
        if(systemLogQuery.getModuleType()!=null &&systemLogQuery.getModuleType()!=-1){
            query.addEq(SystemLog.COLUMN_MODULETYPE,systemLogQuery.getModuleType());
        }
        if(systemLogQuery.getCreateName()!=null && !"".equals(systemLogQuery.getCreateName())){
            query.addLike(SystemLog.COLUMN_CREATENAME,systemLogQuery.getCreateName());
        }
        if(systemLogQuery.getCreateTimeRange()!=null && !"".equals(systemLogQuery.getCreateTimeRange())){
            query.addBetween(SystemLog.COLUMN_CREATETIME, DateUtil.getIntegerTime(systemLogQuery.getCreateTimes()), DateUtil.getIntegerTime(systemLogQuery.getCreateTimee()));
        }else{
            query.addGt(SystemLog.COLUMN_CREATETIME,DateUtil.getCurrentTime()-DateUtil.SECONDS_OF_YEAR);
        }
        query.addOrder(SystemLog.COLUMN_CREATETIME, Query.DBOrder.DESC);
        query.setPaged(systemLogQuery.getPageNo(), systemLogQuery.getPageSize());
        Paged<SystemLog> systemLogPaged =  findPagedByQuery(query) ;
        setNames(systemLogPaged);
        return systemLogPaged;
    }

    public void setNames(Paged<SystemLog> systemLogPaged) {
        if(systemLogPaged.getListData()!=null){
            for(SystemLog log:systemLogPaged.getListData()){
                log.setCreateTimeShow(DateUtil.getFormatDate(log.getCreateTime()));
                log.setModuleTypeName(SystemLog.ModuleTypeEnum.getByValue(log.getModuleType()).tag);
                log.setOperateTypeName(SystemLog.OperateTypeEnum.getByValue(log.getOperateType()).tag);
            }
        }
    }
}
